from PyQt5.QtWidgets import QMainWindow, QFileDialog
from NNclient.Ui_ZFNetwin import Ui_ZFNetwin
from NNclient.myQThread import FC_HDRM_Work
from Data_Pack import myPack


class MyMainWindow(QMainWindow, Ui_ZFNetwin):
    def __init__(self, args, parent=None):
        # 窗口初始化
        super(MyMainWindow, self).__init__(parent)
        self.setupUi(self)
        # 针对既定参数进行填入
        if len(args.host):
            self.LE_serverIP.setText(args.host)
        self.SB_serverPort.setValue(args.port)
        # 构建通信线程模型
        self.myWork = FC_HDRM_Work()
        # 连接线程信号
        self.myWork.sig_confin.connect(self.Con_finish)
        self.myWork.sig_dsfin.connect(self.Dset_finish)
        self.myWork.sig_samfin.connect(self.Sam_finish)
        self.myWork.sig_wffin.connect(self.Wf_finish)
        self.myWork.sig_nntfin.connect(self.NNt_finish)
        self.myWork.sig_nnsfin.connect(self.NNs_finish)
        self.myWork.sig_nnvcfpfin.connect(self.NNvcfp_finish)
        self.myWork.sig_nngsfin.connect(self.NNgs_finish)
        self.myWork.sig_nngdfin.connect(self.NNgd_finish)
        self.myWork.sig_logfin.connect(self.Log_finish)

    def closeEvent(self, event):
        if self.myWork.tcp_on:
            self.myWork.tcp_text = {'cmd': 'close'}
            self.myWork.sc_c.sendall(myPack(self.myWork.tcp_text))
            self.myWork.sc_c.close()
        event.accept()

    def Interact_ena(self, ena=True):
        ena1 = ena and self.myWork.tcp_on
        ena2 = ena1 and self.myWork.dset_on
        ena3 = ena1 and (not self.myWork.nn_workon)
        ena4 = ena2 and (not self.myWork.nn_workon)
        ena5 = ena2 and self.myWork.nn_lok
        # 一般交互
        self.PB_connect.setEnabled(ena)
        self.PB_dload.setEnabled(ena3)
        self.SB_rcount.setEnabled(ena4)
        self.PB_swfile.setEnabled(ena4)
        self.PB_lwfile.setEnabled(ena4)
        self.PB_log.setEnabled(ena4)
        # 训练/验证
        self.PB_nntrain.setEnabled(ena2)
        self.PB_nnstop.setEnabled(ena2)
        self.PB_nnvcfp.setEnabled(ena2)
        self.PB_nngs.setEnabled(ena2)
        self.PB_nngd.setEnabled(ena2)
        self.SB_depoch.setEnabled(ena5)

    def Con_clicked(self):
        self.Interact_ena(False)
        if self.myWork.tcp_on:
            self.LE_state.setText('正在断开……')
        else:
            self.LE_state.setText('正在连接……')
            self.LE_serverIP.setEnabled(False)
            self.SB_serverPort.setEnabled(False)
            self.myWork.tcp_ip = self.LE_serverIP.text()
            self.myWork.tcp_port = self.SB_serverPort.value()
        self.myWork.thr_state = 1
        self.myWork.start()

    def Con_finish(self):
        if self.myWork.tcp_on:
            self.PB_connect.setText('断开')
        else:
            self.PB_connect.setText('连接')
            self.LE_serverIP.setEnabled(True)
            self.SB_serverPort.setEnabled(True)
        self.LE_state.setText(str(self.myWork.thr_msg))
        self.LE_state.home(False)
        self.Interact_ena(True)

    def Dload_clicked(self):
        self.Interact_ena(False)
        self.LE_state.setText('正在发送载入指令……')
        self.myWork.cfp_path = self.LE_cfproot.text()
        self.myWork.thr_state = 2
        self.myWork.start()

    def Dset_finish(self):
        if self.myWork.dset_on:
            self.SB_rcount.setMaximum(self.myWork.rcfp_len - 1)
        self.LE_state.setText(str(self.myWork.thr_msg))
        self.LE_state.home(False)
        self.Interact_ena(True)

    def Rcount_vchanged(self, sam_idx):
        self.Interact_ena(False)
        self.myWork.sam_idx = sam_idx
        self.myWork.thr_state = 3
        self.myWork.start()

    def Sam_finish(self):
        if self.myWork.sam_ok:
            self.L_image.setPixmap(self.myWork.sam_img)
            self.LE_a_count.setText(str(self.myWork.sam_a_idx))
            self.LE_p_count.setText(str(self.myWork.sam_p_idx))
            self.LE_n_count.setText(str(self.myWork.sam_n_idx))
            self.LE_p_tar.setText(self.myWork.sam_p_str)
            self.LE_n_tar.setText(self.myWork.sam_n_str)
        else:
            self.LE_state.setText(str(self.myWork.thr_msg))
            self.LE_state.home(False)
        self.Interact_ena(True)
        self.SB_rcount.setFocus()

    def Sdir_clicked(self):
        wfile = QFileDialog.getSaveFileName(
            self, '选择保存位置', './', '所有文件(*.*)')[0]
        self.LE_swfile.setText(wfile)

    def Swfile_clicked(self):
        self.Interact_ena(False)
        self.LE_state.setText('正在请求权重文件……')
        self.myWork.nn_wsave = True
        self.myWork.nn_wpath = self.LE_swfile.text()
        self.myWork.thr_state = 4
        self.myWork.start()

    def Ldir_clicked(self):
        wfile = QFileDialog.getOpenFileName(
            self, '选择读取位置', './', '所有文件(*.*)')[0]
        self.LE_lwfile.setText(wfile)

    def Lwfile_clicked(self):
        self.Interact_ena(False)
        self.LE_state.setText('正在读取权重文件……')
        self.myWork.nn_wsave = False
        self.myWork.nn_wpath = self.LE_lwfile.text()
        self.myWork.thr_state = 4
        self.myWork.start()

    def Wf_finish(self):
        self.LE_state.setText(str(self.myWork.thr_msg))
        self.LE_state.home(False)
        self.Interact_ena(True)

    def NNt_clicked(self):
        self.Interact_ena(False)
        self.LE_nnstate.setText('正在请求神经网络训练……')
        self.myWork.nnt_epoch = self.SB_tepoch.value()
        self.myWork.thr_state = 5
        self.myWork.start()

    def NNt_finish(self):
        self.LE_nnstate.setText(str(self.myWork.thr_msg))
        self.LE_nnstate.home(False)
        self.Interact_ena(True)

    def NNs_clicked(self):
        self.Interact_ena(False)
        self.LE_nnstate.setText('正在请求神经网络中止……')
        self.myWork.thr_state = 6
        self.myWork.start()

    def NNs_finish(self):
        self.LE_nnstate.setText(str(self.myWork.thr_msg))
        self.LE_nnstate.home(False)
        self.Interact_ena(True)

    def NNvcfp_clicked(self):
        self.Interact_ena(False)
        self.LE_nnstate.setText('正在用CFP对网络进行验证')
        self.myWork.thr_state = 7
        self.myWork.start()

    def NNvcfp_finish(self):
        self.LE_nnstate.setText(str(self.myWork.thr_msg))
        self.LE_nnstate.home(False)
        self.Interact_ena(True)

    def NNgs_clicked(self):
        self.Interact_ena(False)
        self.LE_nnstate.setText('正在获取网络状态……')
        self.myWork.thr_state = 8
        self.myWork.start()

    def NNgs_finish(self):
        self.LE_nnstate.setText(str(self.myWork.thr_msg))
        self.LE_nnstate.home(False)
        self.Interact_ena(True)

    def NNgd_clicked(self):
        self.Interact_ena(False)
        self.LE_nnstate.setText('正在获取网络数据……')
        self.myWork.thr_state = 9
        self.myWork.start()

    def NNgd_finish(self):
        self.LE_nnstate.setText(str(self.myWork.thr_msg))
        self.LE_nnstate.home(False)
        if self.myWork.nn_aok:
            for i in range(11):
                self.TW_nnacr.item(0, i).setText('{:d}'.format(self.myWork.a_num[i]))
                self.TW_nnacr.item(1, i).setText('{:d}'.format(self.myWork.a_cor[i]))
                self.TW_nnacr.item(2, i).setText('{:.2f}%'.format(self.myWork.a_rate[i] * 100.00))
        if self.myWork.nn_lok:
            self.SB_depoch.setMaximum(self.myWork.a_depoch)
            self.SB_depoch.setValue(1)
            self.Depoch_vchanged(self.SB_depoch.value())
        self.Interact_ena(True)

    def Depoch_vchanged(self, decount):
        if self.myWork.nn_lok:
            self.LE_nnloss.setText('{:.4f}'.format(self.myWork.a_losses[decount - 1]))

    def Log_clicked(self):
        self.Interact_ena(False)
        self.LE_state.setText('正在写入log文件……')
        self.myWork.log_dir = self.LE_log.text()
        self.myWork.thr_state = 10
        self.myWork.start()

    def Log_finish(self):
        self.LE_state.setText('写入log完成')
        self.Interact_ena(True)
